# -*- coding: utf-8 -*-
# @Time    : 2024/11/18 10:31
# @Author  : wangwei
# @File    : client.py
import requests as requests

from common.logger import GetLogger


class RequestsClient:
    # 使用类属性来定义个session，他将作为所有接口发起的全局对象
    session = requests.session()
    xx=None

    def __init__(self):
        self.logger = GetLogger.logger
        self.session = RequestsClient.session
        self.headers = None
        self.method = None
        self.data = None
        self.params = None
        self.json = None
        self.files = None
        self.url = None
        self.resp = None
        self.apiname=None
        self.testcase_num=1
        self.testapi_num=1


        # self.aes = AesEncrypt(key='1234567890123456')
        # self.rsa = RsaEncrypt(public_file='public_key.store',private_file='private_key.store')

    # 表示接口调用
    def send(self, **kwargs):
        self.logger.info(f"============================【{self.apiname}】执行开始了================================")
        # 注意：并不是所有接口是data参数，也可能是其他参数
        # 判断下接口请求的各项信息，在kwargs中有没有，如果没有就用对象自身各个属性
        if 'url' not in kwargs:
            kwargs['url'] = self.url
        if 'method' not in kwargs:
            kwargs['method'] = self.method
        if 'headers' not in kwargs:
            kwargs['headers'] = self.headers
        if 'data' not in kwargs:
            kwargs['data'] = self.data
            # kwargs['data'] = self.aes.encrypt(self.data)
            # kwargs['data'] = self.rsa.encrypt_data(self.data)
        if 'json' not in kwargs:
            kwargs['json'] = self.json
            # kwargs['json'] = self.aes.encrypt(self.json)
            # kwargs['json'] = self.rsa.encrypt_data(self.json)
        if 'params' not in kwargs:
            kwargs['params'] = self.params
            # kwargs['params'] = self.aes.encrypt(self.params)
            # kwargs['params'] = self.rsa.encrypt_data(self.params)
        if 'files' not in kwargs:
            kwargs['files'] = self.files
        # 记录接口相关的请求信息
        for key, value in kwargs.items():
            self.logger.info(f'接口的{key}是:{value}')
        try:
            self.resp = self.session.request(**kwargs)
            self.logger.info(f'接口响应状态码:{self.resp.status_code}')
            self.logger.info(f'接口响应body信息:{self.resp.text}')
        except BaseException as e:
            self.logger.exception('接口调用异常')
            raise BaseException(f'接口调用异常:{e}')
        finally:
            self.logger.info(f"============================【{self.apiname}】执行结束了================================")
        return self.resp
